<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <common-item1
          :query="query"
          :streetList="streetList"
          :communityList="communityList"
          :villageList="villageList"
          :show="false"
          @visibleChangeStreet="visibleChangeStreet"
          @changeStreet="changeStreet"
          @visibleChangeCommunity="visibleChangeCommunity"
          @changeCommunity="changeCommunity"
          @visibleChangeVillage="visibleChangeVillage"
          @changeVillage="changeVillage"
        />

        <!-- <el-select
          v-model="query.address"
          placeholder="地址"
          class="handle-select mr10"
        >
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select> -->
        <!-- <el-input
          v-model="query.name"
          placeholder="用户名"
          class="handle-input mr10"
        ></el-input> -->
        <!-- <el-input
          v-model="query.villageName"
          placeholder="小区名字"
          class="handle-input "
        ></el-input> -->
        <el-button
          type="primary"
          style="margin-left: 10px;"
          @click="handleSearch"
          >查询</el-button
        >
        <el-button type="primary" class="handle-del " @click="handleAdd"
          >新增</el-button
        >
        <el-button type="danger" class="handle-del " @click="delAllSelection"
          >删除</el-button
        >

        <el-button type="primary" class="handle-del " @click="handleExport"
          >导出</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="index"
          label="排序"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="argot"
          label="小区暗号"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="subDistrict"
          label="街道/镇名称"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="communityName"
          label="社区名称"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="villageName"
          label="小区名称"
        ></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="editForm" :model="editForm" label-width="70px">
        <el-form-item label="暗号">
          <el-input v-model="editForm.argot"></el-input>
        </el-form-item>
        <el-form-item label="小区">
          <!-- <el-input v-model="editForm.address"></el-input> -->
          <common-item1
            :query="query"
            :streetList="streetList"
            :communityList="communityList"
            :villageList="villageList"
            :show="false"
            @visibleChangeStreet="visibleChangeStreet"
            @changeStreet="changeStreet"
            @visibleChangeCommunity="visibleChangeCommunity"
            @changeCommunity="changeCommunity"
            @visibleChangeVillage="visibleChangeVillage"
            @changeVillage="changeVillage"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="50%">
      <el-form ref="addForm" :model="addForm" label-width="auto">
        <el-form-item label="暗号">
          <el-input v-model="addForm.argot"></el-input>
        </el-form-item>
        <el-form-item label="小区">
          <!-- <el-input v-model="addForm.address"></el-input> -->
          <common-item1
            :query="query"
            :streetList="streetList"
            :communityList="communityList"
            :villageList="villageList"
            :show="false"
            @visibleChangeStreet="visibleChangeStreet"
            @changeStreet="changeStreet"
            @visibleChangeCommunity="visibleChangeCommunity"
            @changeCommunity="changeCommunity"
            @visibleChangeVillage="visibleChangeVillage"
            @changeVillage="changeVillage"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { commonItem1 } from "../../common/commonItem1/common-item1.js";
import NProgress from "nprogress";
export default {
  name: "wechatreplycode",
  data() {
    return {
      query: {
        address: "",
        name: "",
        pageIndex: 1,
        pageSize: 20,

        streetId: "",
        communityId: "",
        villageId: "",

        streetName: "",
        communityName: "",
        villageName: "",

        villageName: "",
      },
      streetList: [],
      communityList: [],
      villageList: [],

      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      addVisible: false,
      pageTotal: 0,
      //   form: {},
      editForm: {
        id: "",
        argot: "",
        villageId: "",
        villageName: "",
      },
      addForm: {
        argot: "",
      },
      idx: -1,
      id: -1,
    };
  },
  created() {
    this.getData();
  },
  components: {
    "common-item1": () => import("../../common/commonItem1/common-item1.vue"),
  },
  methods: {
    // 导出
    handleExport() {
      NProgress.start();
      this.$axios
        .get("/wx/villageargot/export", {
          params: {
            // subDistrictId: this.query.streetId,
            subDistrictId: this.query.streetId,
            communityId: this.query.communityId,
            villageId: this.query.villageId,
            villageName: this.query.villageName,
          },
          responseType: "blob",
        })
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "用户_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          NProgress.done();
        })
        .catch((error) => {
          this.$Notice.error({ title: "错误", desc: "网络连接错误" });
        });
    },
    // 新增
    handleAdd() {
      this.addVisible = true;
      this.query.villageId = "";
      this.query.communityId = "";
      this.query.streetId = "";
      this.query.villageName = "";
      this.query.communityName = "";
      this.query.streetName = "";

      this.streetList = [];
      this.communityList = [];
      this.villageList = [];

      this.addForm.argot = "";
    },
    saveAdd() {
      this.$axios
        .post("/wx/villageargot", {
          argot: this.addForm.argot,
          villageId: this.query.villageId,
          villageName: this.query.villageName,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message.success("新增成功");
          this.query.villageId = "";
          this.query.communityId = "";
          this.query.streetId = "";
          this.query.villageName = "";
          this.query.communityName = "";
          this.query.streetName = "";

          this.streetList = [];
          this.communityList = [];
          this.villageList = [];

          this.addForm.argot = "";
          this.addVisible = false;
          this.getData();
        })
        .catch((err) => {});
    },
    // 编辑操作 有问题 无法根据小区id获得社区数据、街道数据
    handleEdit(index, row) {
      console.log(index, row);
      this.idx = index;
      //   this.form = row;
      this.editVisible = true;
      this.getEdit(row.id);
    },
    getEdit(id) {
      this.$axios
        .get(`/wx/villageargot/${id}`)
        .then(({ data: res }) => {
          console.log("编辑数据", res);
          if (res.code != 0) {
            return this.$message.error(res.msg);
          }
          this.editForm = {
            argot: res.data.argot,
            id: res.data.id,
            villageId: res.data.villageId,
            villageName: res.data.villageName,
          };

          // this.query.streetId = res.data.subDistrictId;
          // this.query.streetName = res.data.subDistrict;
          // this.query.communityId = res.data.communityId;
          // this.query.communityName = res.data.communityName;
          // this.query.villageId = res.data.villageId;
          // this.query.villageName = res.data.villageName;
        })
        .catch(() => {});
    },
    // 保存编辑
    saveEdit() {
      //   this.editVisible = false;
      //   this.$message.success(`修改第 ${this.idx + 1} 行成功`);
      //   this.$set(this.tableData, this.idx, this.form);
      // if (this.query.villageId == "") {
      //   return this.$message.warning({
      //     message: "请选择小区",
      //     duration: 1500,
      //   });
      // }
      this.$axios
        .put("/wx/villageargot", {
          id: this.editForm.id,
          argot: this.editForm.argot,
          villageId: this.query.villageId || this.editForm.villageId,
          villageName: this.query.villageName || this.editForm.villageName,
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return this.$message.error({
              message: res.msg,
              duration: 15000,
            });
          }
          this.editVisible = false;
          this.getData();
        })
        .catch((err) => {});
    },
    ...commonItem1,
    // 获取 easy-mock 的模拟数据
    getData() {
      this.$axios
        .get("/wx/villageargot/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            subDistrictId: this.query.streetId,
            communityId: this.query.communityId,
            villageId: this.query.villageId,
            villageName: this.query.villageName,
          },
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          res.data.list.map((item, index) => {
            var pageIndex = this.query.pageIndex;
            item.index = index + 1 + (pageIndex - 1) * this.query.pageSize;
          });
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;
        })
        .catch((err) => {});
    },
    // 触发查询按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.$message.success("删除成功");
          this.tableData.splice(index, 1);
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = "";
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + " ";
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: auto;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
